from PyQt5.QtWidgets import QTableWidgetItem, QLabel, QHBoxLayout, QWidget, QMenu, QMessageBox
from PyQt5.QtCore import Qt, QPoint

import pyperclip

from common.logger import logger
from common.utils import get_file_size_str
from common.models import FileList
from api.share import ShareAction

class MainShareTable():
	def __init__(self, main_win):
		self._main_win = main_win

		self._share_list = None

		self.initWidgets()

	def initWidgets(self):
		self._main_win.comboBoxShareType.insertItem(0, '发出的分享', 1)
		self._main_win.comboBoxShareType.insertItem(1, '收到的分享', 2)
		self._main_win.comboBoxShareType.setCurrentIndex(0)

		self._main_win.pushButtonShareRefresh.clicked.connect(self.onClickedShareRefresh)

		self._main_win.tableWidgetShareFiles.horizontalHeader().setFixedHeight(50) ##设置表头高度
		self._main_win.tableWidgetShareFiles.horizontalHeader().resizeSection(0, 300) #设置列的宽度
		self._main_win.tableWidgetShareFiles.horizontalHeader().resizeSection(1, 150) #设置列的宽度
		self._main_win.tableWidgetShareFiles.horizontalHeader().resizeSection(2, 100) #设置列的宽度
		self._main_win.tableWidgetShareFiles.horizontalHeader().resizeSection(3, 100) #设置列的宽度

		self._main_win.tableWidgetShareFiles.horizontalHeader().setSectionsClickable(False) #可以禁止点击表头的列

		self._main_win.tableWidgetShareFiles.setContextMenuPolicy(Qt.CustomContextMenu)  #允许右键产生子菜单
		self._main_win.tableWidgetShareFiles.customContextMenuRequested.connect(self.generateShareOptMenu)  #右键菜单

	########################################################################

	def onShowWindow(self):
		self._main_win.comboBoxShareType.setCurrentIndex(0)

	########################################################################

	def getShareList(self, share_type):
		all_share_list = FileList()
		share_action = ShareAction(self._main_win._sess_wp)
		page = 1
		while True:
			share_list = share_action.list_shared_url(share_type, page)
			if len(share_list) <= 0:
				break
			for share_info in share_list:
				all_share_list.append(share_info)
			page += 1

		print(all_share_list)
		self._main_win.tableWidgetShareFiles.setRowCount(len(all_share_list))
		row = 0
		for share_info in all_share_list:

			fileIconLabel = self._main_win.getFileIconLabel(share_info)

			fileNameLabel = QLabel()
			fileNameLabel.setText(share_info.name)
			fileNameLayout = QHBoxLayout()
			fileNameLayout.setAlignment(Qt.AlignLeft)
			fileNameLayout.addWidget(fileIconLabel)
			fileNameLayout.addWidget(fileNameLabel)
			fileNameWidget = QWidget()
			fileNameWidget.setLayout(fileNameLayout)


			sizeStr = ''
			if share_info.isFolder:
				if int(share_info.id) > 0:
					sizeStr = '文件夹'
				else:
					sizeStr = '系统文件夹'
			else:
				sizeStr = get_file_size_str(share_info.size)
			sizeItem = QTableWidgetItem(sizeStr)
			sizeItem.setTextAlignment(Qt.AlignCenter)

			needPwdItem = QTableWidgetItem('是' if share_info.need_pwd == 1 else '否')
			needPwdItem.setTextAlignment(Qt.AlignCenter)

			downCntItem = QTableWidgetItem(str(share_info.downC))
			downCntItem.setTextAlignment(Qt.AlignCenter)

			ctimeItem = QTableWidgetItem(share_info.ctime)
			ctimeItem.setTextAlignment(Qt.AlignCenter)

			self._main_win.tableWidgetShareFiles.setCellWidget(row, 0, fileNameWidget)
			self._main_win.tableWidgetShareFiles.setItem(row, 1, sizeItem)
			self._main_win.tableWidgetShareFiles.setItem(row, 2, needPwdItem)
			self._main_win.tableWidgetShareFiles.setItem(row, 3, downCntItem)
			self._main_win.tableWidgetShareFiles.setItem(row, 4, ctimeItem)

			row += 1

		self._share_list = all_share_list

	def generateShareOptMenu(self, pos):
		row_num = -1
		for idx in self._main_win.tableWidgetShareFiles.selectionModel().selection().indexes():
			row_num = idx.row()

		if row_num < 0:
			return

		if row_num >= len(self._share_list):
			return

		share_info = self._share_list[row_num]

		menu = QMenu()
		item1 = menu.addAction(u"复制分享链接")
		item2 = None
		if share_info.need_pwd == 1:
			item2 = menu.addAction(u"复制密码")

		action = menu.exec_(self._main_win.tableWidgetShareFiles.mapToGlobal(pos + QPoint(0, 30)))
		if action == item1:
			url = 'https:' + share_info.url
			pyperclip.copy(url)
			QMessageBox.information(self._main_win, "提示", f"已复制分享链接：{url}")
		elif action == item2:
			if share_info.need_pwd == 1:
				pyperclip.copy(share_info.pwd)
				QMessageBox.information(self._main_win, "提示", f"已复制密码：{share_info.pwd}")
		else:
			return

	def onClickedShareRefresh(self):
		share_type = self._main_win.comboBoxShareType.currentData()
		self.getShareList(share_type)